package domain

import (
	"go-weixin/app/usercenter/model"
	"go-weixin/app/usercenter/rpc/internal/dao"
	"go-weixin/app/usercenter/rpc/internal/repo"
	"go-weixin/common/chendb"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/x/errors"
)

type RoleDomain struct {
	roleRepo  repo.RoleRepo
	powerRepo repo.PowerRepo
}

func NewRoleDomain(engine chendb.ChenDb) *RoleDomain {
	return &RoleDomain{
		roleRepo:  dao.NewRoleDao(engine),
		powerRepo: dao.NewPowerDao(engine),
	}
}

func (r RoleDomain) RoleAdd(name string, weight int64, parentId int64) error {
	role := new(model.RoleModel)
	affected, err := r.roleRepo.RoleAdd(name, weight, parentId, role)
	if err != nil {
		return err
	}

	if affected == 0 {
		return errors.New(400, "添加角色失败")
	}
	return nil
}

func (r RoleDomain) RoleExistByName(name string) error {
	has, err := r.roleRepo.RoleExistByName(name)
	if err != nil {
		logx.Info(err)
		return err
	}
	if has {
		return errors.New(400, "权限名已经被注册")
	}
	return nil
}

func (r RoleDomain) RoleUpdateExist(name string, id int64) error {
	has, err := r.roleRepo.RoleUpdateExisByName(name, id)
	if err != nil {
		logx.Info(err)
		return err
	}
	if has {
		return errors.New(400, "权限名已存在")
	}
	return nil
}

func (r RoleDomain) RoldAddPowerExist(roleId int64, powerId int64) error {
	has, err := r.powerRepo.PowerExistById(powerId)
	if err != nil {
		logx.Error(err)
		return err
	}
	if !has {
		return errors.New(400, "权限不存在")
	}
	has, err = r.roleRepo.RoleAddPowerExist(roleId, powerId)
	if err != nil {
		logx.Error(err)
		return err
	}

	if has {
		return errors.New(400, "角色已添加了了此权限")
	}
	return nil
}

func (r RoleDomain) RoleAddPower(roleId int64, powerId int64) error {
	affected, err := r.roleRepo.RoleAddPower(roleId, powerId)
	if err != nil {
		logx.Error(err)
		return err
	}
	if affected == 0 {
		return errors.New(400, "角色添加权限失败")
	}
	return nil
}

func (r RoleDomain) RoleUpdatePowerExiste(roleId int64, newPowerId int64) error {
	has, err := r.powerRepo.PowerExistById(newPowerId)
	if err != nil {
		logx.Info(err)
		return err
	}
	if !has {
		return errors.New(400, "权限不存在")
	}
	has, err = r.roleRepo.RoleUpdatePowerExiste(roleId, newPowerId)
	if err != nil {
		return err
	}
	if has {
		return errors.New(400, "角色已经存在此权限")
	}
	return nil
}

func (r RoleDomain) RoleUpdatePower(roleId int64, oldPowerId int64, newPowerId int64) error {
	affected, err := r.roleRepo.RoleUpdatePower(roleId, oldPowerId, newPowerId)
	if err != nil {
		logx.Error(err)
		return err
	}
	if affected == 0 {
		return errors.New(400, "角色修改权限失败")
	}
	return nil
}
func (r RoleDomain) RoleUpdate(id int64, name string, parentId int64, weight int64) error {
	role := &model.RoleModel{
		Name:     name,
		ParentId: parentId,
		Weight:   weight,
	}
	affected, err := r.roleRepo.RoleUpdate(id, role)
	if err != nil {
		logx.Errorf("role-update-err: %v", err)
		return err
	}

	if affected == 0 {
		logx.Error("role-update-err:affected=0")
		return errors.New(400, "role-update-err:affected=0")
	}
	return nil
}

func (r RoleDomain) RoleList(page int64) ([]model.RoleModel, error) {
	limit := 10
	offset := 10 * (page - 1)
	roleList := make([]model.RoleModel, 0)
	err := r.roleRepo.RoleList(int64(limit), offset, &roleList)
	if err != nil {
		return nil, err
	}
	return roleList, nil
}

func (r RoleDomain) RoleDetail(id int64) (*model.RoleModel, error) {
	role := new(model.RoleModel)
	has, err := r.roleRepo.GetRoleById(id, role)
	if err != nil {
		return nil, err
	}

	if !has {
		return nil, errors.New(400, "查询角色失败")
	}
	return role, nil
}

func (r RoleDomain) RoleGetPower(id int64) ([]model.PowerModel, error) {
	powerList := make([]model.PowerModel, 0)
	err := r.roleRepo.RoleGetPowerById(id, &powerList)
	if err != nil {
		return nil, err
	}
	return powerList, nil
}

func (r RoleDomain) RoleGetUser(id int64) ([]model.UserModel, error) {
	userList := make([]model.UserModel, 0)
	err := r.roleRepo.RoleGetUserById(id, &userList)
	if err != nil {
		return nil, err
	}
	return userList, nil
}
